package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.ClientErr;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.PayErr;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.WeChatBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.result.UserInfoByCode;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.PayForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatCode;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatCodeTenant;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatPayCallback;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.request.GetUserInfoByCodeForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.GetUserInfoBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.vo.response.TongLianPayBackVo;
import com.cloudbroker.bcs.platform.dav.bizapi.t2service.export.WeixinT2BizService;
import com.cloudbroker.bcs.platform.dav.middle.entity.Broker;
import com.cloudbroker.bcs.platform.dav.middle.entity.Tenant;
import com.cloudbroker.bcs.platform.dav.middle.extservice.sms.SMSExtService;
import com.cloudbroker.bcs.platform.dav.middle.extservice.weixin.WeixinService;
import com.cloudbroker.bcs.platform.dav.middle.service.BrokerService;
import com.cloudbroker.bcs.platform.dav.middle.service.TenantService;
import com.cloudbroker.bcs.platform.dav.middle.service.WechatUserService;

@Service
public class WechatBizServiceImpl implements WeChatBizService {
    
    @Resource
    private WeixinT2BizService weixinT2;
    @Resource
    private WechatUserService wechatUserService;
    @Resource
    private SMSExtService smsExtService;
    @Resource
    private WeixinService weixinService;
    @Resource
    private BrokerService brokerService;
    @Resource
    private TenantService tenantService;
    
    /* @Override
     public Map<String, Object> wechatUserQuery(WechatUserForm param) {
         Map<String, Object> paramMap = new HashMap<>();
         paramMap.put(UF30Fields.LOGIN_PASSWORD, "2121hj");
         paramMap.put(UF30Fields.MOBILE_TEL, "15868871597");
         paramMap.put(UF30Fields.EMAIL, "xuyinan227@163.com");
         return VOUtil.genErrorResult("1005");
     }*/
    
    @Override
    public UserInfoByCode getUserinfoByCode(WechatCode param) {
        Broker broker = brokerService.getByKey(param.getBroker_key());
        if (broker == null) {
            return VOUtil.genErrorResult(ClientErr.NO_SUCH_CLIENT, UserInfoByCode.class);
        }
        GetUserInfoByCodeForm getUserInfoByCodeForm = new GetUserInfoByCodeForm();
        getUserInfoByCodeForm.setBroker_id(broker.getBroker_id());
        GetUserInfoBackVo getUserInfoBackVo = weixinT2.getUserinfoByCode(getUserInfoByCodeForm.putCode(param
                .getCode()));
        String errorNo = ValueUtil.getString(getUserInfoBackVo.getError_no());
        if (!errorNo.isEmpty() && !errorNo.equals("0")) {
            return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, UserInfoByCode.class);
        } else {
            return VOUtil.setSuccessResult(new UserInfoByCode(getUserInfoBackVo));
        }
    }

    @Override
    public UserInfoByCode getUserinfoByCodeTenant(WechatCodeTenant param) {
        Tenant tenant = tenantService.getByKey(param.getTenant_key());
        if (tenant == null) {
            return VOUtil.genErrorResult(ClientErr.TENANT_NOT_EXIST, UserInfoByCode.class);
        }
        GetUserInfoByCodeForm getUserInfoByCodeForm = new GetUserInfoByCodeForm();
        getUserInfoByCodeForm.setTenant_id(String.valueOf(tenant.getTenant_id()));
        GetUserInfoBackVo getUserInfoBackVo = weixinT2.getUserinfoByCodeTenant(getUserInfoByCodeForm.putCode(param
                .getCode()));
        String errorNo = ValueUtil.getString(getUserInfoBackVo.getError_no());
        if (!errorNo.isEmpty() && !errorNo.equals("0")) {
            return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, UserInfoByCode.class);
        } else {
            return VOUtil.setSuccessResult(new UserInfoByCode(getUserInfoBackVo));
        }
    }

    @Override
    public TongLianPayBackVo getPayData(PayForm param) {
        TongLianPayBackVo vo = weixinT2.unifiedorder(param);
        String error_no = vo.getError_no();
        if(!"0".equals(error_no)){
            return VOUtil.genErrorResult(PayErr.GOLDCOIN_CHANGE_ERR, TongLianPayBackVo.class);
        }
        return VOUtil.setSuccessResult(vo);
    }
    
    @Override
    public Map<String, Object> payCallback(WechatPayCallback param) {
        return weixinService.payCallback(param);
        /*Map<String, Object> resMap = weixinService.payCallback(map);
        String callBack = weixinService.getUserinfoByCode(resMap);
        HSBCPSBackVO resultBack = JSONObject.parseObject(callBack, HSBCPSBackVO.class);
        if (resultBack.getError_no() != null) {
            return VOUtil.genErrorResult(ClientErr.WX_USERINFO_FAIL, HSBCPSBackVO.class);
        } else {
            return VOUtil.setSuccessResult(resultBack);
        }*/
    }
}
